#include <QDesktopServices>
#include <QUrl>
#include "searchwidget.h"
#include "ui_searchwidget.h"

SearchWidget::SearchWidget(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::SearchWidget)
{
    m_ui->setupUi(this);
    m_visible = true;
    m_searchEngine = NONE;
    hideArea();
}

SearchWidget::~SearchWidget()
{
    delete m_ui;
}

void SearchWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SearchWidget::hideArea()
{
    m_ui->searchFrame->hide();
    m_ui->searchBtn->hide();
    m_ui->searchLineEdit->hide();
    m_visible = false;
}

void SearchWidget::showArea()
{
    m_ui->searchFrame->show();
    m_ui->searchBtn->show();
    m_ui->searchLineEdit->show();
    m_visible = true;
}

bool SearchWidget::isAreaVisible()
{
    return m_visible;
}

void SearchWidget::on_googleBtn_clicked()
{
    if(m_searchEngine == GOOGLE)
    {
        if(isAreaVisible())
            hideArea();
        else
        {
            showArea();
            m_ui->searchLineEdit->setFocus();
        }
    }
    else
    {
        switch(m_searchEngine)
        {
            case YANDEX:
                m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                        break;
            case YAHOO:
                m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                        break;
            case MAIL:
                m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                        break;
            case RAMBLER:
                m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                        break;
            default:
                {
                    m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                    m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                    m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                    m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                }
        }
        if(!isAreaVisible())
            showArea();
        m_searchEngine = GOOGLE;
        m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google.png"));
    }
}

void SearchWidget::on_yandexBtn_clicked()
{
    if(m_searchEngine == YANDEX)
    {
        if(isAreaVisible())
            hideArea();
        else
        {
            showArea();
            m_ui->searchLineEdit->setFocus();
        }
    }
    else
    {
        switch(m_searchEngine)
        {
            case GOOGLE:
                m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                        break;
            case YAHOO:
                m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                        break;
            case MAIL:
                m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                        break;
            case RAMBLER:
                m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                        break;
            default:
                {
                    m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                    m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                    m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                    m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                }
        }
        if(!isAreaVisible())
            showArea();
        m_searchEngine = YANDEX;
        m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex.png"));
    }
}

void SearchWidget::on_yahooBtn_clicked()
{
    if(m_searchEngine == YAHOO)
    {
        if(isAreaVisible())
            hideArea();
        else
        {
            showArea();
            m_ui->searchLineEdit->setFocus();
        }
    }
    else
    {
        switch(m_searchEngine)
        {
            case GOOGLE:
                m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                        break;
            case YANDEX:
                m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                        break;
            case MAIL:
                m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                        break;
            case RAMBLER:
                m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                        break;
            default:
                {
                    m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                    m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                    m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                    m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                }
        }
        if(!isAreaVisible())
            showArea();
        m_searchEngine = YAHOO;
        m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo.png"));
    }
}

void SearchWidget::on_mailBtn_clicked()
{
    if(m_searchEngine == MAIL)
    {
        if(isAreaVisible())
            hideArea();
        else
        {
            showArea();
            m_ui->searchLineEdit->setFocus();
        }
    }
    else
    {
        switch(m_searchEngine)
        {
            case GOOGLE:
                m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                        break;
            case YAHOO:
                m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                        break;
            case YANDEX:
                m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                        break;
            case RAMBLER:
                m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                        break;
            default:
                {
                    m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                    m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                    m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                    m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler-g.png"));
                }
        }
        if(!isAreaVisible())
            showArea();
        m_searchEngine = MAIL;
        m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail.png"));
    }
}

void SearchWidget::on_ramblerBtn_clicked()
{
    if(m_searchEngine == RAMBLER)
    {
        if(isAreaVisible())
            hideArea();
        else
        {
            showArea();
            m_ui->searchLineEdit->setFocus();
        }
    }
    else
    {
        switch(m_searchEngine)
        {
            case GOOGLE:
                m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                        break;
            case YAHOO:
                m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                        break;
            case MAIL:
                m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                        break;
            case YANDEX:
                m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                        break;
            default:
                {
                    m_ui->yandexBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yandex-g.png"));
                    m_ui->yahooBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/yahoo-g.png"));
                    m_ui->mailBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/mail-g.png"));
                    m_ui->googleBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/google-g.png"));
                }
        }
        if(!isAreaVisible())
            showArea();
        m_searchEngine = RAMBLER;
        m_ui->ramblerBtn->setIcon(QIcon(":/srch_engines/res/srch_engines/rambler.png"));
    }
}

void SearchWidget::on_searchLineEdit_returnPressed()
{
    search(m_ui->searchLineEdit->text());
}

void SearchWidget::on_searchBtn_clicked()
{
    search(m_ui->searchLineEdit->text());
}

void SearchWidget::search(QString line)
{
    if(line.isNull())
    {
        m_ui->searchLineEdit->setText(line);
    }
    if(m_searchEngine == NONE)
        on_googleBtn_clicked();
    switch(m_searchEngine)
    {
        case GOOGLE:
        {
            //Google
            QDesktopServices::openUrl(QUrl(QString("http://www.google.com/search?q=%1").arg(m_ui->searchLineEdit->text())));
        }
        break;
        case YANDEX:
        {
            // Yandex
            QDesktopServices::openUrl(QUrl(QString("http://yandex.ru/yandsearch?text=%1").arg(m_ui->searchLineEdit->text())));
        }
        break;
        case MAIL:
        {
            // Mail
            QDesktopServices::openUrl(QUrl(QString("http://go.mail.ru/search?q=%1").arg(m_ui->searchLineEdit->text())));
        }
        break;
        case YAHOO:
        {
            // Yahoo
            QDesktopServices::openUrl(QUrl(QString("http://search.yahoo.com/search?p=%1").arg(m_ui->searchLineEdit->text())));
        }
        break;
        case RAMBLER:
        {
            // Rambler
            QDesktopServices::openUrl(QUrl(QString("http://nova.rambler.ru/search?query=%1").arg(m_ui->searchLineEdit->text())));
        }
        break;
        default:
            QMessageBox::critical(this, "Can't search in web", QString("Some error occured").arg(m_ui->searchLineEdit->text()));
    }
}
